package cn.acewill.yxd.yunxundian.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.YXDAPP;
import cn.acewill.yxd.yunxundian.componet.MajorProgress;
import cn.acewill.yxd.yunxundian.model.RMain;
import cn.acewill.yxd.yunxundian.model.tempLostRR;

/**
 * Created by xxx on 2018/1/23.
 */

public class LeftMenuAdapter extends BaseAdapter{
    protected YXDAPP yxdapp=new YXDAPP();
    protected DbManager db = x.getDb(yxdapp.getDaoConfig());
    private Context context;
    private List<RMain.TablesBean.Q1Bean> q1BeanList;
    private int selectItem = 0;
    private List<RMain> rMains;
    private int schId;
    private HashMap<Integer,String> hashMap = new HashMap<>();
    public LeftMenuAdapter(Context context,List<RMain.TablesBean.Q1Bean> q1BeanList,List<RMain> rMains,int schId){
        this.context = context;
        this.q1BeanList = q1BeanList;
        this.rMains = rMains;
        this.schId = schId;
    }
    @Override
    public int getCount() {
        return q1BeanList!=null?q1BeanList.size():0;
    }

    @Override
    public Object getItem(int position) {
        return q1BeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    public void setSelectItem(int position){
        selectItem = position;
    }

    //大项分值
    public void setValueMap(HashMap<Integer,String> valueMap){
        hashMap = valueMap;
    }
    //刷新数据
    public void updateData(int position, ListView listView){
            View view = listView.getChildAt(position);
            ViewHolder holder = (ViewHolder) view.getTag();
            holder.progress = view.findViewById(R.id.majorProgress);
            setData(position,holder);
    }

    private void setData(int index,ViewHolder holder){
        setAdapterData(index,holder);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.left_menu_item,parent,false);
            holder = new ViewHolder();
            holder.progress = convertView.findViewById(R.id.majorProgress);
            holder.indicator = convertView.findViewById(R.id.indicator);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        if (selectItem == position){
            holder.indicator.setVisibility(View.VISIBLE);
        }else{
            holder.indicator.setVisibility(View.GONE);
        }

        setAdapterData(position,holder);

        return convertView;
    }
    class ViewHolder{
        MajorProgress progress;
        View indicator;
    }

    //设置数据
    private void setAdapterData(int position, ViewHolder holder){
        RMain.TablesBean.Q1Bean bean = q1BeanList.get(position);
        if (hashMap!=null&&!hashMap.isEmpty()){
            DecimalFormat df = new DecimalFormat("######0.00");
            for (Map.Entry<Integer,String> entry:hashMap.entrySet()){
                if (entry.getKey() == bean.getID()){
                    String name = entry.getValue();
                    double ss = Double.parseDouble(name);
                    holder.progress.setScoreValue(df.format(ss));
                }
            }
        }else{
            holder.progress.setScoreValue(0+"");
        }
        holder.progress.setMajorName(bean.getReportTableName());
        holder.progress.setItemCount("共"+getMinCount(position)+"项");
        holder.progress.setEditValue("已评"+getWriteItemCount(position)+"项");
        //    holder.progress.setProgress(calculatePrecent(position));
        holder.progress.invalidate();//刷新数据,view's bound damage
    }

    private int getMinCount(int position){
        int count = 0;
        List<RMain.TablesBean.RowsBean> rowlist=rMains.get(0).getTables().get(position).getRows();
        if (rowlist!=null&&rowlist.size()>0){
            for (int j =0;j<rowlist.size();j++){
                count++;
            }
        }return count;
    }

    private int getWriteItemCount(int position){

        int writeCount = 0;
        List<RMain.TablesBean.RowsBean> rowlist=rMains.get(0).getTables().get(position).getRows();
        if (rowlist!=null&&rowlist.size()>0){
            for (int j =0;j<rowlist.size();j++){
                try {
                    tempLostRR rr = db.selector(tempLostRR.class).where("schId","=",schId )
                            .and("rRowId","=",rowlist.get(j).getQ2().getID()).findFirst();
                    if (rr!=null){
                        writeCount++;
                    }
                } catch (DbException e) {
                    e.printStackTrace();
                }
            }
        }
        Log.e("notifiyCount",writeCount+"");
        return writeCount;
    }

    // 0/0 = gg   1000为了保存一位小数
    private int calculatePrecent(int position){
        if (getMinCount(position)>0){
            Log.e("notifyprecent",getWriteItemCount(position)/getMinCount(position)*100+"");
            return getWriteItemCount(position)*1000/getMinCount(position);
        }
        return 0;
    }

}
